package com.star.carservice.client.fragment;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.star.carservice.R;
import com.star.carservice.client.activity.SelectCarActivity;
import com.star.carservice.client.adapter.CarBrandListAdapter;
import com.star.carservice.client.adapter.CarSeriesListAdapter;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carserviceapi.domain.CarFeatureInfo;
import com.star.carserviceapi.domain.request.QryCarBrandRequest;
import com.star.carserviceapi.domain.request.QryCarSeriesRequest;
import com.star.carserviceapi.domain.result.QryCarBrandResult;
import com.star.carserviceapi.domain.result.QryCarSeriesResult;

public class SelectCarSeriesFragment extends BaseFragment {

	private final static String TAG = SelectCarSeriesFragment.class
			.getSimpleName();	

	public static SelectCarSeriesFragment getInstance(CarFeatureInfo info) {
		SelectCarSeriesFragment f = new SelectCarSeriesFragment();

		Bundle bundle = new Bundle();
		bundle.putSerializable(SelectCarActivity.KEY_CAR, info);

		f.setArguments(bundle);

		return f;
	}

	@Override
	public String getLogTag() {
		return TAG;
	}

	@InjectView(R.id.list_car_brand)
	ListView list_car_brand;
	List<String> mData = new ArrayList<String>();
	CarSeriesListAdapter mAdapter = null;

	CarFeatureInfo mCarFeatureInfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mCarFeatureInfo = (CarFeatureInfo) getArguments().getSerializable(
				SelectCarActivity.KEY_CAR);

		execAsyncTask(new QryCarSeriesTask(), new QryCarSeriesRequest(
				mCarFeatureInfo.getBrand()));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		if (CacheViewIsNull()) {
			mCacheView = inflater.inflate(R.layout.frag_select_car_series,
					container, false);

			ButterKnife.inject(this, mCacheView);

			initView();
		}

		removeCacheView();

		return mCacheView;
	}

	private void initView() {
		mAdapter = new CarSeriesListAdapter(getActivity(), mCarFeatureInfo,
				mData);
		list_car_brand.setAdapter(mAdapter);

		list_car_brand.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				mCarFeatureInfo.setSeries((String) mAdapter.getItem(position));

				getActivity()
						.getSupportFragmentManager()
						.beginTransaction()
						.replace(
								R.id.view_placeholder,
								SelectCarModelFragment
										.getInstance(mCarFeatureInfo))
						.addToBackStack(null).commit();

			}
		});
	}

	class QryCarSeriesTask extends
			AsyncTask<QryCarSeriesRequest, Void, QryCarSeriesResult> {

		@Override
		protected QryCarSeriesResult doInBackground(
				QryCarSeriesRequest... params) {

			return getApi().qryCarSeries(params[0]);
		}

		@Override
		protected void onPostExecute(QryCarSeriesResult result) {
			mData.clear();
			mData.addAll(result.getData());
			mAdapter.notifyDataSetChanged();
		}
	}

}
